package com.ruoyi.alipay.service.impl;

import com.ruoyi.alipay.mapper.PaymentSettlementMapper;
import com.ruoyi.alipay.domain.PaymentSettlement;
import com.ruoyi.alipay.service.PaymentSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PaymentSettlementServiceImpl implements PaymentSettlementService {
    
    /**
     * 查询支付列表
     * @param payment 查询条件
     * @return 支付列表
     */
    @Override
    public List<PaymentSettlement> selectPaymentList(PaymentSettlement payment) {
        return paymentSettlementMapper.selectPaymentList(payment);
    }
    
    /**
     * 查询支付详情
     * @param id 支付ID
     * @return 支付详情
     */
    @Override
    public PaymentSettlement selectPaymentById(Long id) {
        return paymentSettlementMapper.selectPaymentById(id);
    }

    // 注入 PaymentSettlementMapper
    @Autowired
    private PaymentSettlementMapper paymentSettlementMapper;

    /**
     * 插入一条支付结算记录
     * @param payment
     * @return
     */
    @Override
    public int insert(PaymentSettlement payment) {
        return paymentSettlementMapper.insert(payment);
    }

    /**
     * 根据outTradeNo查询支付结算记录
     * @param outTradeNo
     * @return
     */
    @Override
    public PaymentSettlement findByOutTradeNo(String outTradeNo) {
        return paymentSettlementMapper.findByOutTradeNo(outTradeNo);
    }

    /**
     * 根据outTradeNo更新支付结算记录
     * @param payment
     * @return
     */
    @Override
    public int updateByOutTradeNo(PaymentSettlement payment) {
        return paymentSettlementMapper.updateByOutTradeNo(payment);
    }

    /**
     * 根据userId和productName查询支付结算记录
     * @param userId
     * @param productName
     * @return
     */
    @Override
    public PaymentSettlement findByUserIdAndProductName(Integer userId, String productName){
        return paymentSettlementMapper.findByUserIdAndProductName(userId, productName);
    }

    /**
     * 根据userId查询支付结算记录
     * @param userId
     * @return
     */
    @Override
    public List<String> findPayCoursesByUserId(Integer userId) {
        List<PaymentSettlement> settlements = paymentSettlementMapper.findByUserId(userId);
        return settlements.stream()
                .map(PaymentSettlement::getProductName)
                .distinct() // 确保商品名称唯一
                .collect(Collectors.toList());
    }
} 